<?php

defined( 'IN_DICE' ) or die( 'Access Error!' );

include_once( DIR_MDL . 'platform/platform.php' );
include_once( DIR_MDL . 'platform/class.renren.php' );

class renren implements platform {
	
	/**
	 * @var XNapp
	 */
	public $op;
	public $pfid;
	public $apiid;
	public $apikey;
	public $apisecret;
	public $apiurl;
	
	public function __construct() {
		$this->pfid = all::$cfg['renren_pfid'];
		$this->apiid = all::$cfg['renren_apiid'];
		$this->apikey = all::$cfg['renren_apikey'];
		$this->apisecret = all::$cfg['renren_apisecret'];
		$this->apiurl = all::$cfg['renren_apiurl'];
		$this->op = new XNapp( $this->apikey, $this->apisecret );
	}
	
	/**
	 * 获取当前登录用户的平台ID.字符串型,没有获取到则为空字符串
	 */
	public function getUserId() {
		$uid = $this->op->users('getLoggedInUser');
		return strlen( $uid[0][0] ) ? $uid[0][0] : '';
	}
	
	public function getUserInfo( $uid ) {
		if( !strlen( $uid ) ) {
			return array();
		}
		$params = array (
	    			"uids"	=> array( $uid),
	    			"fields"=> array('uid','name','sex','birthday','tinyurl','headurl','mainurl','work_history','hometown_location','university_history','star'),
					);
		$uinfo = $this->op->users('getInfo',$params);
		
		$uinfo = $uinfo['user']; //二维数组
		
		return ( strcmp($uid, $uinfo['uid']) !== 0) ? array() : array( //错误则返回空数组,否则严格返回以下格式
																	'puid' => $uinfo['uid'],
																	'uname' => $uinfo['name'],
																	'headurl' => $uinfo['headurl'],
																	'sex' => $uinfo['sex'],
																	'psuid' => '',
																	'upage' => 'http://renren.com/profile.do?id=' . $uinfo['uid'],
																	'usid' => 0, //用户的子ID,用于同城或者本站
																	'pfid' => all::$cfg['renren_pfid'],
																	);
	}
	public function getAllFriends(){
		$list = $this->op->friends('get');
		return is_array($list['uid']) ? $list['uid'] : array();
	}
	public function getAppFriends(){
		$list = $this->op->friends('getAppUsers');
		return is_array($list['uid']) ? $list['uid'] : array();
	}
	
	/**
	 * 应用授权，用户是否添加此应用或已登录
	 */
	public function isAdded() {
		$xn_sig_added = (int)$_REQUEST['xn_sig_added'];
		if ( ($xn_sig_added == 0 ) || ( !isset($_REQUEST['xn_sig_session_key'] ) ) ) {
			$xn_sig_domain = $_REQUEST['xn_sig_domain'];
			$xn_sig_session_key = $_REQUEST['xn_sig_session_key'];

			$img = all::$aOut['imgurl'] . '2.jpg';
			$apiurl = all::$cfg['renren_apiurl'];
			echo "<script type=\"text/javascript\">
	 				var callback = function(){ document.setLocation(\"{$apiurl}\"); }
	 				var cancel = function(){ document.setLocation(\"http://app.{$xn_sig_domain}/apps/apps.do\"); }
	  				Session.requireLogin(callback, cancel);
				</script>";
			echo "<img src=\"$img\" width=\"760\">";
			exit;
		}
	}
	public function getSiteInfo() {
		
	}
	public function sendNotice() {
		
	}
	public function sendFeed( $aFields ) {
		
	}
	public function setProfile() {
		
	}
	public function setKey( $key ) {
		$this->op->session_key = $key;
	}
  public function getpayurl() {
    return '';
  }
}
